from PyQt4.QtGui import * 
from PyQt4.QtCore import * 

class BrushList(QWidget):
    """Brushes!"""
    def __init__(self, sprite_editor):
        QWidget.__init__(self, None)
        self.vbox = QVBoxLayout()
        self.list = QListWidget()

        self.sprite_editor = sprite_editor

        self.brushes = {
                Qt.SolidPattern : "Solid",
                Qt.Dense1Pattern : "Dense 1",
                Qt.Dense2Pattern : "Dense 2",
                Qt.Dense3Pattern : "Dense 3",
                Qt.Dense4Pattern : "Dense 4",
                Qt.Dense5Pattern : "Dense 5",
                Qt.Dense6Pattern : "Dense 6",
                Qt.Dense7Pattern : "Dense 7",
                Qt.HorPattern : "Horizontal",
                Qt.VerPattern : "Vertical",
                Qt.CrossPattern : "Cross",
                Qt.BDiagPattern : "B. Diagonal",
                Qt.FDiagPattern : "F. Diagonal",
                Qt.DiagCrossPattern : "Diagonal Cross"};

        for key in self.brushes.keys():
            print key

        self.vbox.addWidget(self.list)

        self.setLayout(self.vbox)

        self.initialize_dialog()
        self.initialize_list()

    def initialize_dialog(self):
        self.connect(self.list, SIGNAL("currentRowChanged(int)"), self.row_changed)

    def initialize_list(self):
        """Clear and (re-)initialize list elements."""
        self.list.clear()
        for num,brush_description in self.brushes.iteritems():
            item = QListWidgetItem(str(brush_description))
            pixmap = QPixmap(25,25)

            p = QPainter(pixmap)
            p.fillRect(self.rect(),QBrush(QColor("white")))
            p.fillRect(pixmap.rect(),QBrush(num))
            p.end()

            icon = QIcon()
            icon.addPixmap(pixmap)
            item.setIcon(icon)
            item.setSizeHint(QSize(25,25))

            self.list.addItem(item)

        # Set current index to selected tile id.
        self.list.setCurrentRow(self.sprite_editor.brush_id-1, QItemSelectionModel.Select)


    def row_changed(self, ev):
        if self.list.currentRow() >= 0:
	        self.sprite_editor.set_brush(self.list.currentRow()+1)
